﻿using System;
using System.Collections.Generic;
using Model;

namespace BusinessObject
{
    public class ReleaseBO : Repository<Release>
    {
        #region Singleton

        private static readonly ReleaseBO _instance = new ReleaseBO();

        /// <summary>
        /// Instância do objeto singleton.
        /// </summary>
        public static ReleaseBO Instance
        {
            get { return _instance; }
        }

        private ReleaseBO() { }

        #endregion Singleton

        public void InsertRelease(Project project, string name, DateTime startDate, DateTime endDate)
        {
            Release release = new Release();

            release.Nome = name;
            release.DataInicio = startDate;
            release.DataFim = endDate;
            release.Project = project;

            ReleaseBO.Instance.Save(release);
        }

        public bool CheckDates(Project project, DateTime startDate, DateTime endDate)
        {
            if (((project.DataInicio <= startDate) && (startDate <= project.DataFim)) && ((project.DataInicio <= endDate) && (endDate <= project.DataFim)) && (startDate <= endDate))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IList<Release> GetReleaseByProject(Project project)
        {
            IList<Release> lista = base.GetByCriteria(r => r.Project.Id == project.Id);
            return lista;
        }

        public IList<Release> GetReleaseByProjectByDate(Project project, DateTime date)
        {
            IList<Release> lista = base.GetByCriteria(r => r.Project.Id == project.Id && r.DataInicio >= date);
            return lista;
        }

        public bool VerifyReleaseSameDate(Project project, Release release)
        {
            foreach (Release releaseItem in base.GetByCriteria(r => r.Project.Id == project.Id))
            {
                if (release.DataInicio == releaseItem.DataInicio || release.DataFim == releaseItem.DataFim)
                    return false;
            }
            return true;
        }
    }
}